package org.example.listener.event;

import lombok.SneakyThrows;
import org.example.data.WorkEvent;
import org.example.listener.WorkApplicationEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MsgListener {

    @SneakyThrows // 该注解可以省略代码中一些抛异常或者try catch操作（建议少用，此处写个简单的小demo偷懒）
    @Async // 开启异步监听
    @EventListener(WorkApplicationEvent.class)
    public void sendMsg(WorkApplicationEvent workApplicationEvent) {
        // 具体监听的业务逻辑
        WorkEvent workEvent = (WorkEvent)workApplicationEvent.getSource();
        if (workEvent.getState() == 1) {
            Thread.sleep(1000);
            System.out.println(workEvent.getContents());
        } else {
            throw new RuntimeException("抛出异常，观察后续执行情况");
        }
    }
}
